Skip to content

Add Builder pattern to structs#60

Open
ju6ge wants to merge 5 commits intosoftprops:masterfrom
ju6ge:master
Open

Add Builder pattern to structs#60
ju6ge wants to merge 5 commits intosoftprops:masterfrom
ju6ge:master

Conversation

@ju6ge
Copy link

@ju6ge ju6ge commented Jul 13, 2022

This PR uses the derive_builder crate to derive builder classes for all structs. This enables writing an OpenApi Spec in a much more readable and straight forward fashion (No need to explicitly set parts of the struct to None or creating a mutable struct via default and then just editing the desired fileds). This is useful when using this crate to generate an openapi spec instead of parsing it from a file.

Example with Builders:

use openapi::OpenApi;
use openapi::v3_0::{ComponentsBuilder, ContactBuilder, InfoBuilder, OperationBuilder, ParameterBuilder, PathItemBuilder, ServerBuilder, SpecBuilder};

let spec = SpecBuilder::default()
	.openapi("3.0.1".to_string())
	.info(InfoBuilder::default()
			.title("Example Api".to_string())
			.version(clap::crate_version!().to_string())
			.contact(ContactBuilder::default()
					.name("Support".to_string())
					.email("mail@example.com".to_string())
					.build().unwrap()
			)
			.build().unwrap()
	)
	.servers(vec![ ServerBuilder::default()
					.url("https://api.example.com")
					.build().unwrap()
			]
	)
	.paths(api_paths)
	.build().unwrap();

OpenApi::V3_0(spec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant